package kr.hyunist.androidcomplete01saeildamil.ch12;

import java.util.*;

import kr.hyunist.androidcomplete01saeildamil.*;
import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class CustomAdapterEx2 extends Activity {
	ArrayList<MyWeatherInfo> arList;
	MyWeatherInfo mwInfo;
	MyWeatherAdapter adapter;
	ListView list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.customadapterex);

		arList = new ArrayList<MyWeatherInfo>();

		mwInfo = new MyWeatherInfo("02:00", false, R.drawable.dark);
		arList.add(mwInfo);
		mwInfo = new MyWeatherInfo("07:10", true, R.drawable.normal);
		arList.add(mwInfo);
		mwInfo = new MyWeatherInfo("07:00", true, R.drawable.normal);
		arList.add(mwInfo);
		mwInfo = new MyWeatherInfo("06:30", true, R.drawable.normal);
		arList.add(mwInfo);

		adapter = new MyWeatherAdapter(CustomAdapterEx2.this,
				R.layout.customadapterex2contents, arList);

		list = (ListView) findViewById(R.id.customeadapterexlistview);
		list.setAdapter(adapter);
		list.setOnItemClickListener(mClickListener);
		list.setOnItemLongClickListener(mLongClickListener);

	}

	OnItemClickListener mClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			if(arList.get(position).isEnable()){
				arList.get(position).setEnable(false);
			} else {
				arList.get(position).setEnable(true);
			}
			adapter.notifyDataSetChanged();
			
			/*boolean enable = arList.get(position).isEnable();
			arList.get(position).setEnable(!enable);
			adapter.notifyDataSetChanged();*/
		}

	};

	OnItemLongClickListener mLongClickListener = new OnItemLongClickListener() {

		@Override
		public boolean onItemLongClick(AdapterView<?> parent, View view,
				int position, long id) {
			arList.remove(position);
			adapter.notifyDataSetChanged();
			Toast.makeText(CustomAdapterEx2.this, "삭제 완료", Toast.LENGTH_SHORT)
					.show();
			return true;
		}

	};
}

class MyWeatherInfo {
	private String time;
	private boolean enable;
	private int Background;

	public MyWeatherInfo(String time) {
		this.time = time;
	}

	public MyWeatherInfo(String time, boolean enable, int background) {
		super();
		this.time = time;
		this.enable = enable;
		Background = background;
	}

	public String getTime() {
		return time;
	}

	public boolean isEnable() {
		return enable;
	}

	public int getBackground() {
		return Background;
	}

	public void setTime(String time) {
		this.time = time;
	}

	public void setEnable(boolean enable) {
		this.enable = enable;
	}

	public void setBackground(int background) {
		Background = background;
	}

}

class MyWeatherAdapter extends BaseAdapter {
	ArrayList<MyWeatherInfo> arWeather;
	Context context;
	int layout;
	LayoutInflater inflater;

	public MyWeatherAdapter(Context mContext, int mLayout,
			ArrayList<MyWeatherInfo> arList) {
		super();
		context = mContext;
		layout = mLayout;
		arWeather = arList;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return arWeather.size();
	}

	@Override
	public Object getItem(int position) {
		return arWeather.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final int pos = position;

		if (convertView == null) {
			convertView = inflater.inflate(layout, parent, false);
		}

		convertView.setBackgroundResource(arWeather.get(pos).getBackground());

		TextView time = (TextView) convertView
				.findViewById(R.id.weathercustomadaptertimetv);
		time.setText(arWeather.get(pos).getTime());

		Button btn = (Button) convertView
				.findViewById(R.id.weathercustomadaptertbtn);

		if (arWeather.get(pos).isEnable()) {
			btn.setBackgroundResource(R.drawable.clark_green);
		} else {
			btn.setBackgroundResource(R.drawable.clark_gray);
		}

		return convertView;
	}

}